class Solution(object):
    def containsNearbyDuplicate(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: bool
        """
        map_pos = {}
        for (i, c) in enumerate(nums):
            if c not in map_pos:
                map_pos[c] = i
            else:
                if (i - map_pos[c]) <= k:
                    return True
                else:
                    map_pos[c] = i

        return False
        
s = Solution()
print(s.containsNearbyDuplicate([1,2,3,1], 3)) # t